package cn.com.yang.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 映射缓冲区
 * @Author 杨亿金
 * @Date 2021/12/19 20:32
 * @CopyRight
 * @Version
 */
public class MappedBuffer {
    static private final int start =0;
    static private final int size = 1024;
    public static void main(String[] args) {
        try {
            RandomAccessFile raf = new RandomAccessFile("E://test0.txt","rw");
            FileChannel channel = raf.getChannel();
            //把缓冲区跟文件系统进行一个映射关联
            //只要操作缓冲区里面的内容，文件内容也会跟着改变
            MappedByteBuffer mbb =channel.map(FileChannel.MapMode.READ_WRITE,start,size);
            mbb.put(0,(byte)97);
            mbb.put(1023,(byte)122);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){

        }
    }
}
